package com.wmwj.hxlj.mapper;

import com.wmwj.hxlj.entity.Collection;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface CollectionMapper {
    /**
     * 插入收藏记录
     * @param collection
     * @return
     */
    @Insert("INSERT INTO collection(user_id, collection_id, resource_id,status) " +
            "VALUES(#{userId}, #{collectionId}, #{resourceId},#{status})")
    int insert(Collection collection);

    /**
     * 根据资源id查询收藏记录
     * @param userId
     * @param collectionId
     * @param resourceId
     * @return
     */
    @Select("SELECT * FROM collection WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND resource_id = #{resourceId}")
    Collection findById(Long userId,Long collectionId,Long resourceId);

    /**
     * 更新收藏状态
     * @param collection
     * @return
     */
    @Update("UPDATE collection SET status = #{status} WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND resource_id = #{resourceId}")
    int updateStatus(Collection collection);

    /**
     * 删除收藏记录(软删除）
     * @param collection
     * @return
     */
    @Update("UPDATE collection SET status = 1 WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND resource_id = #{resourceId}")
    int deleteStatus(Collection collection);

    /**
     * 插入收藏记录(软添加）
     * @param collection
     * @return
     */
    @Update("UPDATE collection SET status = 0 WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND resource_id = #{resourceId}")
    int insertStatus(Collection collection);

    /**
     * 根据用户id和收藏夹id查询收藏记录
     * @param userId
     * @param collectionId
     * @return
     */
    @Select("SELECT * FROM collection WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND status = 0 "+
            "LIMIT #{pageStart},#{pageSize}")
    List<Collection> findByUserIdAndCollectionId(Long userId, Long collectionId, int pageStart,int pageSize);

    /**
     * 根据用户id和收藏夹id查询收藏记录总数
     * @param userId
     * @param collectionId
     * @return
     */
    @Select("SELECT COUNT(*) FROM collection WHERE user_id = #{userId} " +
            "AND collection_id = #{collectionId} AND status = 0 ")
    int findByUserIdAndCollectionIdCount(Long userId, Long collectionId);
}
